package com.supermap.localization;

import java.io.File;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.supermap.commontypes.PropertiesInfo;
import com.supermap.commontypes.SampleInfo;
import com.supermap.component.LocalizationComponent;
import com.supermap.component.MatcherComponent;
import com.supermap.component.SampleComponent;
import com.supermap.component.impl.LocalizationComponentImpl;
import com.supermap.component.impl.MatcherComponentImpl;
import com.supermap.component.impl.SampleComponentImpl;
import com.supermap.dao.SampleDao;
import com.supermap.dao.impl.SampleDaoImpl;
import com.supermap.impl.PropertiesHelper;
import com.supermap.logger.LoggerUtil;

public class LocalizationImpl {

    private static Logger logger = Logger.getLogger(LocalizationImpl.class);

    private static PropertiesInfo propertiesInfo = new PropertiesInfo();

    private static LocalizationComponent localizationComponent;
    
    private static SampleDao sampleDao;
    
    private static SampleComponent sampleComponent;
    
    public static void main(String[] args) {

        if (args.length == 0) {
            System.out.println("请输入操作参数");
            return;
        }
        try {
            if(args[0].equalsIgnoreCase("extract")) {
                initExtractConfig();
                LoggerUtil.initLogger(propertiesInfo.getLogDir());
                MatcherComponent matcherComponent = new MatcherComponentImpl();
                localizationComponent = new LocalizationComponentImpl(propertiesInfo.getBeforeExcludePatterns(), propertiesInfo.getPatterns(), propertiesInfo.getAfterExcludePatterns(), matcherComponent);
                Set<SampleInfo> sampleInfos = localizationComponent.extractSample(propertiesInfo.getSrcDir());
                sampleDao = new SampleDaoImpl(propertiesInfo.getSampleOutputFile(), propertiesInfo.getSampleInfoFile());
                sampleComponent = new SampleComponentImpl(sampleDao);
                sampleComponent.saveSample(sampleInfos);
                logger.info("提取样本成功！");
                System.out.println("提取样本成功！");
            }else{
                initUpdateConfig();
                LoggerUtil.initLogger(propertiesInfo.getLogDir());
                MatcherComponent matcherComponent = new MatcherComponentImpl();
                sampleDao = new SampleDaoImpl(propertiesInfo.getSampleFile(), propertiesInfo.getSampleInfoFile());
                sampleComponent = new SampleComponentImpl(sampleDao);
                List<SampleInfo> sampleInfos = sampleComponent.readSample();
                localizationComponent = new LocalizationComponentImpl(propertiesInfo.getBeforeExcludePatterns(), propertiesInfo.getPatterns(), propertiesInfo.getAfterExcludePatterns(), matcherComponent);
                localizationComponent.updateKeyToFile(propertiesInfo.getSrcDir(), sampleInfos, propertiesInfo.getDestDir());
                logger.info("替换文本成功！");
                System.out.println("替换文本成功！");
            }
            
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            System.out.println("操作失败，详细信息请查看日志");
        }
    }

    private static void initExtractConfig() throws Exception {
        propertiesInfo.setConfigPath(new File("").getAbsolutePath() + "\\" + propertiesInfo.getConfigPath());
        Properties prop = PropertiesHelper.loadProperties(propertiesInfo.getConfigPath());
        propertiesInfo.setSrcDir(StringUtils.trim(prop.getProperty("srcDir")));
        propertiesInfo.setSampleOutputFile(StringUtils.trim(prop.getProperty("sampleOutputFile")));
        propertiesInfo.setSampleInfoFile(StringUtils.trim(prop.getProperty("sampleInfoFile")));
        propertiesInfo.setLogDir(StringUtils.trim(prop.getProperty("logDir")));
        String patternsProp = prop.getProperty("patterns");
        if (patternsProp != null && StringUtils.isNotBlank(patternsProp)) {
            String[] patterns = patternsProp.split(";");
            propertiesInfo.setPatterns(patterns.clone());
        }
        String beforeExcludePatternsProp = prop.getProperty("beforeExcludePatterns");
        if (beforeExcludePatternsProp != null) {
            String[] excludePatterns = beforeExcludePatternsProp.split(";");
            if (excludePatterns.length > 0 && StringUtils.isNotBlank(beforeExcludePatternsProp)) {
                propertiesInfo.setBeforeExcludePatterns(excludePatterns.clone());
            }
        }
        String afterExcludePatternsProp = prop.getProperty("afterExcludePatterns");
        if (afterExcludePatternsProp != null && StringUtils.isNotBlank(afterExcludePatternsProp)) {
            String[] excludePatterns = afterExcludePatternsProp.split(";");
            if (excludePatterns.length > 0) {
                propertiesInfo.setAfterExcludePatterns(excludePatterns.clone());
            }
        }
        PropertiesHelper.checkExtractPropertiesInfo(propertiesInfo);
    }

    private static void initUpdateConfig() throws Exception {
        propertiesInfo.setConfigPath(new File("").getAbsolutePath() + "\\" + propertiesInfo.getConfigPath());
        Properties prop = PropertiesHelper.loadProperties(propertiesInfo.getConfigPath());
        propertiesInfo.setSrcDir(StringUtils.trim(prop.getProperty("srcDir")));
        propertiesInfo.setDestDir(StringUtils.trim(prop.getProperty("destDir")));
        propertiesInfo.setSampleFile(StringUtils.trim(prop.getProperty("sampleFile")));
        propertiesInfo.setLogDir(StringUtils.trim(prop.getProperty("logDir")));
        String beforeExcludePatternsProp = prop.getProperty("beforeExcludePatterns");
        if (beforeExcludePatternsProp != null) {
            String[] excludePatterns = beforeExcludePatternsProp.split(";");
            if (excludePatterns.length > 0 && StringUtils.isNotBlank(beforeExcludePatternsProp)) {
                propertiesInfo.setBeforeExcludePatterns(excludePatterns.clone());
            }
        }
        PropertiesHelper.checkUpdatePropertiesInfo(propertiesInfo);
    }

}
